package com.frost.book.config;

import com.frost.book.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

// 拦截器，这里是强制用户登录后才能访问
@Configuration
public class WebConfig implements WebMvcConfigurer {
    private List<String> excludePathPatterns = Arrays.asList("/user/login", "/**/login.html", "/css/**", "/js/**", "/pic/**");

    @Autowired
    private LoginInterceptor loginInterceptor;

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // /** 表示对所有的路径生效
//        registry.addInterceptor(loginInterceptor).addPathPatterns("/**")
//                // 排除一些路径
//                .excludePathPatterns("/user/login")
//                .excludePathPatterns("/**/**.html")
//                .excludePathPatterns("/css/**")
//                .excludePathPatterns("/js/**")
//                .excludePathPatterns("/pic/**");
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // /** 表示对所有的路径生效
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePathPatterns);
    }
}
